/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUI.java
 *
 * Created on Oct 6, 2010, 10:49:28 PM
 */

package cat200project;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author akmal
 */
public class CASH_SUMMARY extends javax.swing.JFrame {

    Statement stat;
    Connection con;
    ResultSet rset;
    private String url = "jdbc:oracle:thin:@localhost:1521:XE";
    private String pass_id = "restaurant";
    private String pass_pw = "restaurant";
    String newline = "\n";
    Double price;
    Double total = 0.00;

    /** Creates new form GUI */
    public CASH_SUMMARY() throws SQLException {
        super("SUMMARY");
        initComponents();
        this.setLocationRelativeTo(null);
        connect();
        //populateData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ItemArea = new javax.swing.JTextArea();
        doneButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        TableNum = new javax.swing.JComboBox();
        PaymentType = new javax.swing.JComboBox();
        Total = new javax.swing.JTextField();
        CashPay = new javax.swing.JTextField();
        Balance = new javax.swing.JTextField();
        goButton = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jSeparator4 = new javax.swing.JSeparator();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText(" ITEM PURCHASED");
        jLabel6.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabel6.setBounds(300, 50, 180, 40);
        jLayeredPane1.add(jLabel6, javax.swing.JLayeredPane.DEFAULT_LAYER);

        ItemArea.setColumns(20);
        ItemArea.setFont(new java.awt.Font("Times New Roman", 1, 12));
        ItemArea.setRows(5);
        ItemArea.setFocusable(false);
        jScrollPane1.setViewportView(ItemArea);

        jScrollPane1.setBounds(310, 89, 160, 220);
        jLayeredPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        doneButton.setFont(new java.awt.Font("Times New Roman", 1, 12));
        doneButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        doneButton.setText("DONE");
        doneButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        doneButton.setBounds(370, 350, 100, 40);
        jLayeredPane1.add(doneButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        backButton.setFont(new java.awt.Font("Times New Roman", 1, 12));
        backButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        backButton.setText("BACK");
        backButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });
        backButton.setBounds(250, 350, 100, 40);
        jLayeredPane1.add(backButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        TableNum.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        TableNum.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010" }));
        TableNum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TableNumActionPerformed(evt);
            }
        });
        TableNum.setBounds(130, 90, 90, 30);
        jLayeredPane1.add(TableNum, javax.swing.JLayeredPane.DEFAULT_LAYER);

        PaymentType.setFont(new java.awt.Font("Times New Roman", 1, 12));
        PaymentType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "Cash", "Credit Card" }));
        PaymentType.setBounds(130, 140, 90, 30);
        jLayeredPane1.add(PaymentType, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Total.setFont(new java.awt.Font("Times New Roman", 1, 12));
        Total.setFocusable(false);
        Total.setBounds(130, 190, 90, 20);
        jLayeredPane1.add(Total, javax.swing.JLayeredPane.DEFAULT_LAYER);

        CashPay.setFont(new java.awt.Font("Times New Roman", 1, 12));
        CashPay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CashPayActionPerformed(evt);
            }
        });
        CashPay.setBounds(130, 230, 90, 20);
        jLayeredPane1.add(CashPay, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Balance.setFont(new java.awt.Font("Times New Roman", 1, 12));
        Balance.setFocusable(false);
        Balance.setBounds(130, 270, 90, 20);
        jLayeredPane1.add(Balance, javax.swing.JLayeredPane.DEFAULT_LAYER);

        goButton.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        goButton.setText("Go!");
        goButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                goButtonActionPerformed(evt);
            }
        });
        goButton.setBounds(230, 90, 70, 30);
        jLayeredPane1.add(goButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel5.setText("BALANCE :");
        jLabel5.setBounds(20, 270, 70, 14);
        jLayeredPane1.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel4.setText("CASH PAY :");
        jLabel4.setBounds(20, 230, 70, 14);
        jLayeredPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel3.setText("TOTAL :");
        jLabel3.setBounds(20, 190, 70, 14);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel2.setText("PAYMENT TYPE :");
        jLabel2.setBounds(20, 140, 100, 20);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel1.setText("MENU CODE :");
        jLabel1.setBounds(20, 90, 110, 20);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator1.setBounds(0, 50, 500, 10);
        jLayeredPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator2.setBounds(0, 340, 500, 10);
        jLayeredPane1.add(jSeparator2, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator3.setBounds(20, 260, 230, 10);
        jLayeredPane1.add(jSeparator3, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator4.setBounds(20, 300, 230, 10);
        jLayeredPane1.add(jSeparator4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        jLabel7.setText("jLabel7");
        jLabel7.setBounds(0, 0, 500, 400);
        jLayeredPane1.add(jLabel7, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void CashPayActionPerformed(java.awt.event.ActionEvent evt) {                                        

        String pay = CashPay.getText();
        Double payD = Double.parseDouble(pay);
        String tot = Total.getText();
        Double totD = Double.parseDouble(tot);
        Double balance = payD - totD;
        Balance.setText(balance.toString());

        // TODO add your handling code here:
    }                                       

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

        new EMPLOYEE_MENU().setVisible(true);
        this.setVisible(false);

        // TODO add your handling code here:
    }                                          

    private void TableNumActionPerformed(java.awt.event.ActionEvent evt) {                                         

        // TODO add your handling code here:
    }                                        

    private void goButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         

        String a = (String) TableNum.getSelectedItem();

        if(a.equals("Select"))
        {
            JOptionPane.showMessageDialog(null, "Select Menu Code First!");
        }
        else
        {
            try
            {
                populateData(a);
            }
            catch (SQLException ex)
            {
                Logger.getLogger(CASH_SUMMARY.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

       // TODO add your handling code here:
    }                                        

    private void populateData(String z) throws SQLException
    {
        clear();
        ItemArea.setText("");
        String query = "SELECT FOOD_NAME,FOOD_PRICE FROM ORDER_LIST WHERE MENU_CODE='"+z+"'";
        rset = getResultSet(query);
        while(rset.next())
        {
            ItemArea.append(rset.getString("FOOD_NAME") + newline);
            price = Double.parseDouble(rset.getString("FOOD_PRICE"));
            total += price;
        }
        Total.setText(total.toString());
    }

    private ResultSet getResultSet(String s) throws SQLException {
        connect();
        ResultSet rs = null;
        try {
            stat = con.createStatement();
            rs = stat.executeQuery(s);
        } catch (SQLException ex) {
            Logger.getLogger(CASH_SUMMARY.class.getName()).log(Level.SEVERE, null, ex);
        }

        return rs;
    }

    private void connect() throws SQLException
    {
         try {
            //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
            System.out.println("Driver Loaded");
            con = DriverManager.getConnection(url,pass_id,pass_pw);
            System.out.println("Database Connected");

         }
         catch (Exception ex)
         {
            Logger.getLogger(CASH_SUMMARY.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void clear()
    {
        total = 0.00;
        Total.setText("");
        CashPay.setText("");
        Balance.setText("");
        PaymentType.setSelectedIndex(0);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new CASH_SUMMARY().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(CASH_SUMMARY.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JTextField Balance;
    private javax.swing.JTextField CashPay;
    private javax.swing.JTextArea ItemArea;
    private javax.swing.JComboBox PaymentType;
    private javax.swing.JComboBox TableNum;
    private javax.swing.JTextField Total;
    private javax.swing.JButton backButton;
    private javax.swing.JButton doneButton;
    private javax.swing.JButton goButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    // End of variables declaration                   

}
